package dao.impl;


import com.mysql.cj.util.StringUtils;
import dao.SmbmsProviderDao;
import pojo.SmbmsProvider;
import pojo.SmbmsRole;
import utils.BaseDao;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SmbmsProviderDaoImpl extends BaseDao implements SmbmsProviderDao {
    @Override
    //通过用户编码查询用户信息
    public SmbmsProvider queryProviderByProCode(String proCode) throws Exception {
        SmbmsProvider  smbmsProvider=new SmbmsProvider();
        String sql="select  * from smbms_provider where proCode=?";
        Object[] params={proCode};
        ResultSet resultSet = executeSQL(sql, params);
        while(resultSet.next()){
            smbmsProvider.setId(resultSet.getLong("id"));
            smbmsProvider.setProCode(resultSet.getString("proCode"));
            smbmsProvider.setProName(resultSet.getString("proName"));
            smbmsProvider.setProDesc(resultSet.getString("proDesc"));
        }

        return smbmsProvider;
    }
    @Override
    //查询供应商信息并支持模糊查询
    public List<SmbmsProvider> queryProviderByProName(String proName) throws Exception {
        List<SmbmsProvider> list = new ArrayList<SmbmsProvider>();
        StringBuffer sql = new StringBuffer();
        List<Object> obj = new ArrayList<Object>();
        sql.append("select  * from smbms_provider");
        if (!StringUtils.isNullOrEmpty(proName)) {
            sql.append(" where proName like ?");
            obj.add("%" + proName + "%");
        }
        Object[] params = obj.toArray();
        System.out.println(sql.toString() + "========================");
        System.out.println(params + "=====================");
        ResultSet resultSet = executeSQL(sql.toString(), params);
        while (resultSet.next()) {
            SmbmsProvider smbmsProvider = new SmbmsProvider();
            smbmsProvider.setId(resultSet.getLong("id"));
            smbmsProvider.setProCode(resultSet.getString("proCode"));
            smbmsProvider.setProName(resultSet.getString("proName"));
            smbmsProvider.setProContact(resultSet.getString("proContact"));
            smbmsProvider.setProPhone(resultSet.getString("proPhone"));
            smbmsProvider.setProFax(resultSet.getString("proFax"));
            smbmsProvider.setCreationDate(resultSet.getDate("creationDate"));
            //把对象放在集合中
            list.add(smbmsProvider);
        }
        return list;
    }
    @Override
    //增加功能
    public int addProvider(SmbmsProvider smbmsProvider) throws Exception {
        int count=0;
        String sql="insert into smbms_provider(proCode, proName,proDesc,proContact, proPhone,proAddress,proFax,createdBy, creationDate)values(?,?,?,?,?,?,?,?,?)";
        Object[] params=
                {smbmsProvider.getProCode(),smbmsProvider.getProName(),smbmsProvider.getProDesc(),smbmsProvider.getProContact(),smbmsProvider.getProPhone(),smbmsProvider.getProAddress(),smbmsProvider.getProFax(),smbmsProvider.getCreatedBy(),smbmsProvider.getCreationDate()};
        count = executeUpdate(sql, params);
        return count;
    }
    //通过id查询用户信息
    @Override
    public SmbmsProvider queryProviderById(Long id) throws Exception {
        SmbmsProvider smbmsProvider=new SmbmsProvider();
        String sql="select *  from smbms_provider where id=?";
        Object[] params={id};
        ResultSet resultSet = executeSQL(sql, params);
        while(resultSet.next()){
            smbmsProvider.setId(resultSet.getLong("id"));
            smbmsProvider.setProCode(resultSet.getString("proCode"));
            smbmsProvider.setProName(resultSet.getString("proName"));
            smbmsProvider.setProContact(resultSet.getString("proContact"));
            smbmsProvider.setProPhone(resultSet.getString("proPhone"));
            smbmsProvider.setProAddress(resultSet.getString("proAddress"));
            smbmsProvider.setProFax(resultSet.getString("proFax"));
            smbmsProvider.setProDesc(resultSet.getString("proDesc"));
        }
        return smbmsProvider;
    }
    //修改操作
    @Override
    public int updateProvider(SmbmsProvider smbmsProvider) throws Exception {
        int count=0;
        String sql="update smbms_provider set proCode=?, proName=?,proContact=?, proPhone=?,proAddress=?,proFax=?,proDesc=? where id=?";
        Object[] params=
                {smbmsProvider.getProCode(),smbmsProvider.getProName(),smbmsProvider.getProContact(),smbmsProvider.getProPhone(),smbmsProvider.getProAddress(),smbmsProvider.getProFax(),smbmsProvider.getProDesc(),smbmsProvider.getId()};
        count= executeUpdate(sql, params);
        return count;
    }
    //删除功能
    @Override
    public int deleteProvider(Long id) throws Exception {
        int count=0;
        String sql="delete from smbms_provider where id=?";
        Object[] params={id};
        count = executeUpdate(sql, params);
        return count;
    }

    @Override
    public List<SmbmsProvider> queryPronName() throws Exception {
            List<SmbmsProvider> list=new ArrayList<SmbmsProvider>();
            String sql="select * from smbms_provider";
            Object[] params={};
            ResultSet resultSet = executeSQL(sql, params);
            while(resultSet.next()){
                SmbmsProvider smbmsProvider=new SmbmsProvider();
                smbmsProvider.setId(resultSet.getLong("id"));
                smbmsProvider.setProName(resultSet.getString("proName"));
//把对象放在集合中
                list.add(smbmsProvider);
            }
            return list;
    }


}

